package com.bean.serializabletest;

import com.bean.serializabletest.model.NettyMessage;
import com.bean.serializabletest.model.NettyMessageBuilder;
import com.bean.serializabletest.serializeutil.AbstractSerialize;
import com.bean.serializabletest.serializeutil.JacksonSerializeUtil;
import com.bean.serializabletest.serializeutil.JdkSerializeUtil;
import com.bean.serializabletest.serializeutil.ProtostuffSerializeUtil;

import java.time.Duration;
import java.time.LocalDateTime;

public class SerializeTest {

    public static void main(String[] args) {
        SerializeTest.testFastJsonSerialize(new ProtostuffSerializeUtil(), 10);

    }

    public static void testFastJsonSerialize(AbstractSerialize serialize, int count){
        LocalDateTime startTime = LocalDateTime.now();
        System.out.println("测试工具为："+serialize.getClass());


        NettyMessage message =  NettyMessageBuilder.build();

        TimeUtil timeUtil = new TimeUtil();
        TimeUtil timeUtil1 = new TimeUtil();

        NettyMessage result = null;
        byte[]  serByte = serialize.serialize(message);
        System.out.println("字节长度:" + serByte.length);
        result  = serialize.deserialize(serByte,NettyMessage.class);
        //这里设置测试次数
        for(int i = 0; i< count; i++){
            //timeUtil.init();
            timeUtil.start();
            serByte = serialize.serialize(message);
            timeUtil.end();
            //System.out.println("序列化时间："+ timeUtil.getAvrTimeUs() + " Us");

            timeUtil1.start();
            result  = serialize.deserialize(serByte,NettyMessage.class);
            timeUtil1.end();


        }
        LocalDateTime endTime = LocalDateTime.now();
        Duration duration = Duration.between(startTime, endTime);
        System.out.println("总共用时："+duration.toMillis()/60f);
        System.out.println("序列化时间："+ timeUtil.getAvrTimeUs() + " Us");
        System.out.println("反序列化时间："+ timeUtil1.getAvrTimeUs() + " Us");

        System.out.println("结果:" + result);

    }


}
